package com.lm.liaoming.http;


import android.content.Context;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 韦敏敏
 */

public class RetrofitManger {


        public static final String URL_TEST="http://test.dadisurvey.com/";//测试
//    public static final String URL_RELEASE = "http://xa.dadisurvey.com/"; //发布
    public static final String URL = URL_TEST;
    public static final String BASE_URL = URL + "dweb/";
    private static Retrofit build = null;
    /*配置okhttp*/
    private static OkHttpClient getClient() {
        return new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS)
                .build();
    }

    /*配置okhttp*/
    private static OkHttpClient getClient2(Context context) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.e("loadSuccess", message + "");
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//设置日志打印等级
        return new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new AddCookiesInterceptor(context))
//                .addInterceptor(new ReceivedCookiesInterceptor(context)) //这部分
                .sslSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory(), SSLSocketFactoryUtils.createTrustAllManager())
                .hostnameVerifier(new SSLSocketFactoryUtils.TrustAllHostnameVerifier())
                .build();
    }

    public static Retrofit initRetrofitJava(Context context) {
        Retrofit build = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(getClient2(context))
                .build();

        return build;
    }


}
